package com.songcha.module_home.ui.fragment.home

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseMvvmFragment
import com.songcha.library_business.bean.configuration.SystemConfigurationResultBean
import com.songcha.library_business.config.FunctionConfig
import com.songcha.library_business.config.VarConfig
import com.songcha.library_business.event.home.PersonalRecommendStateChangeEvent
import com.songcha.library_business.handler.HomeBookRecommendDialogHandler
import com.songcha.library_business.helper.AdvertCoverHelper
import com.songcha.library_business.helper.AdvertHelper
import com.songcha.library_business.helper.UMengHelper
import com.songcha.library_business.helper.WxHelper
import com.songcha.library_business.proxy.configuration.SystemConfigurationManager
import com.songcha.library_business.util.MajiaBaoUtil
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.ui.view.BottomNavigationView
import com.songcha.library_common.util.ClipboardUtil
import com.songcha.library_common.util.DeviceUtil
import com.songcha.library_common.util.JumpUtil
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.PackageUtil
import com.songcha.library_common.util.StatusBarUtil
import com.songcha.library_common.util.TimeUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_common.util.dp2px
import com.songcha.module_home.R
import com.songcha.module_home.bean.HomeCateBean
import com.songcha.module_home.ui.adapter.HomeTabAdapter
import com.songcha.module_home.databinding.HomeFragmentHomeBinding
import com.songcha.module_home.ui.activity.search.SearchActivity
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.util.concurrent.TimeUnit

@Route(path="/home/HomeFragment")
class HomeFragment : BaseMvvmFragment<HomeViewModel,HomeFragmentHomeBinding>(),BottomNavigationView.IOnBottomNavigationItemChangeListener {
    private var mIsCurrentPage = false

    init {
        TAG=HomeFragment::class.java.simpleName
        if(MajiaBaoUtil.getMajiaName() == "douyue"){
            mIsCurrentPage = true
        }else{
            mIsCurrentPage = false
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.home_fragment_home
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        databinding.homeLlSearch.setOnClickListener {
            JumpUtil.toActivity(it.context,SearchActivity::class.java)
        }

        if(MajiaBaoUtil.getMajiaName() != "douyue"){
            ViewHelper.setVisibility(false,databinding.homeCclSearch)
        }else{
            initAdvert()
        }

        EventBus.getDefault().register(this)

    }

    private fun initAdvert(){
        AdvertCoverHelper.loadAdvertByCover(requireActivity(),AdvertHelper.ADVERT_TYPE_INSERT_SCREEN,VarConfig.ADVERT_HOME_INSERT_SCREEN_INFO,
            null,0f,
            object :AdvertHelper.ILoadAdvertCommonListener{
            override fun onSuccess(type: Int, height: Int,view: View?) {

            }

            override fun onError(type: Int,code:Int,msg:String) {

            }

            override fun onClose(type: Int) {

            }

        })
    }

    override fun initObserver() {
        viewModel.homeCateList.observe (this){ data ->
            initVp(data)
        }
    }

    override fun onGetData() {
        viewModel.getHomeCate()
    }


    override fun isNeedShowLoad(): Boolean {
        return true
    }

    @Subscribe
    fun onPersonalRecommendStateChange(event:PersonalRecommendStateChangeEvent){
        onGetData()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }


    /*override fun onApplyWindowInsetsCall(v: View, insets: WindowInsetsCompat){
        if(!isViewModelInitialized()) return
        databinding.homeLlTab.layoutParams=databinding.homeLlTab.layoutParams.apply {
            this as LinearLayout.LayoutParams
            topMargin=StatusBarUtil.getStatusBarHeight(requireActivity())+10.dp2px()
        }
    }*/

    override fun onRootViewPost() {
        databinding.homeLlTab.layoutParams=databinding.homeLlTab.layoutParams.apply {
            this as LinearLayout.LayoutParams
            topMargin=StatusBarUtil.getStatusBarHeight(requireActivity())+10.dp2px()
        }
    }


    private fun initVp(list:MutableList<HomeCateBean.DataBean>){
        if(list.size==0) return
        val tmp = list.toMutableList()
        var homeTitle = "首页"
        if(MajiaBaoUtil.getMajiaName() == "beike"){
            homeTitle = "全部"
        }
        tmp.add(0,HomeCateBean.DataBean(0,homeTitle))

        val vp=databinding.homeVp
        vp.adapter = HomeTabAdapter(childFragmentManager,tmp)
        databinding.homeCtl.setViewPager(vp)
    }

    override fun onResume() {
        super.onResume()
        if(MajiaBaoUtil.getMajiaName() != "douyue" && mIsCurrentPage)
            HomeBookRecommendDialogHandler.handleShowHomeBookRecommendDialog(requireContext()) { it ->
                viewModel.getHomeRecommendDialogBook { it2 ->
                    it.invoke(it2)
                }
            }
    }

    override fun onStop() {
        super.onStop()
        if(MajiaBaoUtil.getMajiaName() != "douyue")
            HomeBookRecommendDialogHandler.handleInterruptHomeBookRecommendDialog()
    }

    override fun onBottomNavigationItemChange(position: Int,tag:String) {
        if(tag==TAG){
            mIsCurrentPage = true
            UMengHelper.onUMEvent(requireContext(), "bottom_tab_main")
            if(MajiaBaoUtil.getMajiaName() != "douyue")
                HomeBookRecommendDialogHandler.handleShowHomeBookRecommendDialog(requireContext()) { it ->
                    viewModel.getHomeRecommendDialogBook { it2 ->
                        it.invoke(it2)
                    }
                }
        }else{
            mIsCurrentPage = false
            if(MajiaBaoUtil.getMajiaName() != "douyue")
                HomeBookRecommendDialogHandler.handleInterruptHomeBookRecommendDialog()
        }

    }

}